package com.cy.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cy.entity.JobCard;
import com.cy.entity.JsonResult;
import com.cy.entity.Meetingroom;
import com.cy.entity.Order_list;
import com.cy.entity.Type;
import com.cy.entity.User;
import com.cy.exception.enums.ErrorEnums;
import com.cy.mapper.UserMapper;
import com.cy.service.JobCardService;
import com.cy.service.Order_listService;
import com.cy.service.UserService;

@RestController
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private JobCardService jobCardService;
	
	@Autowired
	private Order_listService order_listService;
	
	@RequestMapping("findAllUsers")
	public JsonResult findAllUsers() {
		return new JsonResult(userService.findAllUsers());
	}
	
	@RequestMapping("findByUserTelAndAccess")
	//通过权限和电话模糊查询用户
	public JsonResult findByUserTelAndAccess(String tel,Integer access) {
		
		List<User> listl = userService.findByUserTelAndAccess(tel, access);
		return new JsonResult(listl);
	}
	
	
	
	
	@RequestMapping("register")
	public JsonResult register(@RequestBody User user) {
		if (userService.findUserByTel(user.getUsername()).size()==0) {
			return new JsonResult("注册成功");
		}
		return new JsonResult("该用户名已被使用！！");
	}
	
	@RequestMapping("login")
	public JsonResult login(@RequestBody Map<String, String> map) {
		System.out.println(map);
		String tel = map.get("tel");
		String password = map.get("password");
		System.out.println(tel+" "+password);
		if (userService.login(tel, password).size()>0) {
			List<User> user = userService.findByUserTel(tel);
			return new JsonResult(user);
		}
		return new JsonResult(1,"用户名或密码错误！");
	}
	
	@RequestMapping("findJobCardById")
	public JsonResult findJobCardById(String id) {
		JobCard jc = jobCardService.findJobCardById(id);
		return new JsonResult<>(jc);
	
	}
	
	@RequestMapping("authortity")//赋予用户学生/教师权限
	public JsonResult authortity(Integer userId,String jobId,String jobName) {
		System.out.println("userid:"+userId+"jobid:"+jobId+"jobName"+jobName);
		JobCard jc = jobCardService.findJobCardById(jobId);
		User user = userService.findUserById(userId);
		if(jc!=null && jc.getJobname().equals(jobName)) {
			if (jc.getStatus()==1) {
				return new JsonResult<>(1,"该学号/工号已被绑定");
			}
			user.setAccess(1);
			user.setJobid(jc.getJobid());
			jc.setStatus(1);
			jobCardService.updateJobCard(jc);
			userService.updateUserById(user);
			return new JsonResult<>(jc);
		}
		return new JsonResult<>(1,"ID或用户名有误	");
	}
	
	@RequestMapping("authortityByUser")//用户认证学生/教师权限
	public JsonResult authortityByUser(Integer userId,String jobId,String jobName) {
		
		JobCard jc = jobCardService.findJobCardById(jobId);
		User user = userService.findUserById(userId);
		if(jc!=null && jc.getJobname().equals(jobName)) {
			if (jc.getStatus()==1) {
				return new JsonResult<>(1,"该学号/工号已被绑定");
			}
			user.setAccess(1);
			user.setJobid(jc.getJobid());
			jc.setStatus(1);
			jobCardService.updateJobCard(jc);
			userService.updateUserById(user);
			return new JsonResult<>(user);
		}
		return new JsonResult<>(1,"ID或用户名有误	");
	}
	
	@RequestMapping("dePower")//剥夺用户权限
	public JsonResult dePower(Integer userId) {
		User user = userService.findUserById(userId);
		user.setAccess(0);
		JobCard jc = jobCardService.findJobCardById(user.getJobid());
		user.setJobid(null);
		jc.setStatus(0);
		userService.updateUserById(user);
		jobCardService.updateJobCard(jc);
		return new JsonResult<>(0,"success");
	}
	
	@RequestMapping("showUserByAccess")
	//通过权限展现用户
	public JsonResult showUserByAccess() {
		List<Map<String, List<User>>> list = new ArrayList<>();
		Map<String, List<User>> map1 = new HashMap<>();
		List<User> user1 =userService.findUserByAccess(0);
		map1.put("state0", user1);
		Map<String, List<User>> map2 = new HashMap<>();
		List<User> user2 =userService.findUserByAccess(1);
		map2.put("state1", user2);
		list.add(map1);
		list.add(map2);
		return new JsonResult<>(list);
	}
	
	//通过用户iD查询用户信息
	@RequestMapping("findUserById")
	public JsonResult findUserById(Integer userId) {
		return new JsonResult<>(userService.findUserById(userId));
	}
	
	@RequestMapping("editUser")
	public JsonResult editUser(Integer userId, String userName, String password, String userTel) {
		
		User user = userService.findUserById(userId);
		List<User> list = userService.findUserByTel(userTel);
		if(list.size()>0) {
			if(!list.get(0).getUserid().equals(userId)) {
				return new JsonResult<>(1,"该手机号已经被他人注册！");
			}
		}
//		List<Order_list> list1 = order_listService.findOrder_listByUserId(userId);
//		if(list1.size()>0) {
//			return new JsonResult<>(1,"用户有预约存在，不可删除！");			
//		}
		
		user.setUsername(userName);
		user.setPassword(password);
		user.setUsertel(userTel);
		userService.updateUserById(user);
		return new JsonResult<>("修改成功！");
	}
	//用户修改个人信息
	@RequestMapping("editUserBySelf")
	public JsonResult editUserBySelf(Integer userId, String userName, String userTel) {
		
		User user = userService.findUserById(userId);
		List<User> list = userService.findUserByTel(userTel);
		if(list.size()>0) {
			if(!list.get(0).getUserid().equals(userId)) {
				return new JsonResult<>(1,"该手机号已经被他人注册！");
			}
		}
//		List<Order_list> list1 = order_listService.findOrder_listByUserId(userId);
//		if(list1.size()>0) {
//			return new JsonResult<>(1,"用户有预约存在，不可删除！");			
//		}
		
		user.setUsername(userName);
		user.setUsertel(userTel);
		userService.updateUserById(user);
		return new JsonResult<>(user);
	}
	//用户修改密码
	@RequestMapping("editUserBySelfPassword")
	public JsonResult editUserBySelf(Integer userId, String password) {
		
		User user = userService.findUserById(userId);		
		user.setPassword(password);
		userService.updateUserById(user);
		return new JsonResult<>(user);
	}
		
	@RequestMapping("deleteUser")
	public JsonResult deleteUser(Integer id) {
		if(order_listService.findOrder_listByUserId(id).size()>0) {
			return new JsonResult<>(1,"该用户有预约,无法注销！");
		}
		else if (userService.findUserById(id).getJobid()!=null) {
		  JobCard jc = jobCardService.findJobCardById(userService.findUserById(id).getJobid());
		  jc.setStatus(0);
		  jobCardService.updateJobCard(jc);
		  userService.deleteUser(id);
		return new JsonResult<>(0,"success");
		}
		userService.deleteUser(id);
		return new JsonResult<>(0,"success");
	}
}
